package com.dwjm.exam.stu_login;

import com.dwjm.exam.pojo.Student;
import utils.ConnectionDatabaseForMySQLUseJdbc1;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class StudentDaoImpl implements StudentDao{
    Connection connection = null;
    PreparedStatement pstmt = null;
    ResultSet rest = null;

    public StudentDaoImpl() {
        connection = ConnectionDatabaseForMySQLUseJdbc1.getConnection();
    }

    @Override
    public Student Login(String account, String password) {
        try {
            pstmt = connection.prepareStatement("select * from student where username=? and password=?");
            pstmt.setString(1, account);
            pstmt.setString(2, password);
            rest = pstmt.executeQuery();
            if(rest.next()){
                Student student = new Student();
                student.setStudentId(rest.getString("studentId"));
                student.setName(rest.getString("name"));
                student.setGender(rest.getString("gender"));
                student.setBirthDate(rest.getDate("birthDate"));
                student.setIdNumber(rest.getString("idNumber"));
                student.setCollege(rest.getString("college"));
                student.setMajor(rest.getString("major"));
                student.setClassName(rest.getString("className"));
                student.setStatus(rest.getString("status"));
                student.setDormId(rest.getString("dormId"));
                student.setBedNumber(rest.getString("bedNumber"));
                student.setPhone(rest.getString("phone"));
                student.setEmail(rest.getString("email"));
                student.setEmergencyContact(rest.getString("emergencyContact"));
                student.setUserName(rest.getString("username"));
                student.setPhone(rest.getString("password"));
                return student;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
